home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / JOYSTICK.SWG / 0014_More Joystick code.pas < prev   
Pascal/Delphi Source File  |  1995-02-28  |  6KB  |  203 lines

  1. Program Joystick;
  2. uses CRT;
  3.  
  4. const Gameport=$201;
  5.       Timer0=$40;
  6.       TControl=$43;
  7.       MaxLoops=5000;
  8.       Button1=$10; Button2=$20; Button3=$40; Button5=$80;
  9.       Xaxis1=$01; Yaxis1=$02; Xaxis2=$04; Yaxis2=$08;
  10.  
  11. var X,Y:word;
  12. Procedure GetJoy; assembler;
  13. label loop1,loop2,axis1,loop3,axdone;
  14.  
  15. asm
  16.     cli               {disable interrupts}
  17.     mov  dx,Gameport;  {set port adress}
  18.     mov  cx,MaxLoops;
  19.     mov  al,0;
  20.     out  TControl,al;  {latch count in timer0}
  21.     in   al,Timer0;     {low byte of timer count}
  22.     mov  ah,al;
  23.     in   al,Timer0;     {high byte of timer count}
  24.     xchg al,ah;
  25.     mov  bx,ax;        {start count in BX}
  26.     out  dx,al;        {trigger game port}
  27.     in   al,dx
  28. loop1:
  29.     in   al,dx;        {Read Gameport}
  30.     mov  ah,al;
  31.     and  ax,$0201;      {X axis in al; Y axis in ah}
  32.     test al,Xaxis1;      {is X axis done?}
  33.     jz   axis1;
  34.     test ah,Yaxis1;      {is Y axis done?}
  35.     loopnz loop1;
  36.                        {Y axis done first!}
  37.     out  TControl,al;
  38.     in   al,Timer0;     {low byte of Y axis count}
  39.     mov  ah,al;
  40.     in   al,Timer0;     {high byte of Y axis count}
  41.     xchg al,ah
  42.     push ax            {store Y axis count on the stack}
  43. loop2:
  44.     in   al,dx;
  45.     and  al,Xaxis1;
  46.     test al,Xaxis1;      {Test X axis}
  47.     loopnz loop2;
  48.                        {X axis done(after Y)}
  49.     out  TControl,al;
  50.     in   al,Timer0;
  51.     mov  ah,al;
  52.     in   al,Timer0;
  53.     xchg al,ah         {X axis count}
  54.     sub  ax,bx;        {find difference}
  55.     neg  ax
  56.     mov  X,ax;         {Save X axis time}
  57.     pop  ax;           {Get Y axis count}
  58.     sub  ax,bx
  59.     neg  ax
  60.     mov  Y,ax;         {Save Y axis time}
  61.     jmp  axdone        {We're done.}
  62.  
  63. axis1:                 {X axis done first}
  64.     out  TControl,al;
  65.     in   al,Timer0
  66.     mov  ah,al
  67.     in   al,Timer0
  68.     xchg al,ah
  69.     push ax            {Store X axis count on the stack}
  70. loop3:
  71.     in   al,dx
  72.     and  al,Yaxis1;
  73.     test al,Yaxis1;
  74.     loopnz loop3;
  75.                         {Y is done}
  76.     out  TControl,al;
  77.     in   al,Timer0;
  78.     mov  ah,al
  79.     in   al,Timer0
  80.     xchg al,ah
  81.     sub  ax,bx
  82.     neg  ax
  83.     mov  Y,ax           {Save Y axis Time}
  84.     pop  ax             {Get X axis count}
  85.     sub  ax,bx
  86.     neg  ax
  87.     mov  X,ax           {Save X axis count}
  88. axdone:
  89.     sti
  90. end;
  91.  
  92. var b1,b2,b3,b4:byte;                      
  93. Procedure Getbutton; assembler;
  94. label bt2,bt3,bt4,done;
  95.  
  96. asm
  97.     mov b1,0
  98.     mov b2,0
  99.     mov b3,0
  100.     mov b4,0
  101.     mov dx,Gameport;
  102.     in al,dx;
  103.     test al,$10;
  104.     jnz bt2             {there must be a better way to do this}
  105.     mov b1,1
  106. bt2:
  107.     test al,$20;
  108.     jnz bt3
  109.     mov b2,1
  110. bt3:
  111.     test al,$40;
  112.     jnz bt4;
  113.     mov b3,1;
  114. bt4:
  115.     test al,$80;
  116.     jnz done;
  117.     mov b4,1
  118. done:
  119. end;
  120.  
  121. var k:char; done:boolean; MaxX,Minx,MaxY,MinY:word;
  122.     MX,MY:byte;     {percent-adjusted centered joystick values}
  123.     CX,CY,D:byte;   {Cursor positions and loop Delay}
  124.  
  125. Begin
  126.   ClrScr;
  127.   done:=false;
  128.   GetJoy;
  129.   MaxX:=X; MinX:=X; MaxY:=Y; MinY:=Y;    {initial values}
  130.   Writeln('Whip that joystick around until the 4 leftmost numbers stop changing,');
  131.   writeln('then center the joystick and press button 1 or any key.');
  132.   if KeyPressed then k:=ReadKey;   {Clear KeyBuffer}
  133.   while not done do begin
  134.     GetJoy;
  135.     if X>=MaxX then MaxX:=X;      {find the range of the joystick}
  136.     if X<=MinX then MinX:=X;
  137.     if Y>=MaxY then MaxY:=Y;
  138.     if Y<=MinY then MinY:=Y;
  139.     gotoxy(1,5);
  140.     Writeln(MinX,'    ',MaxX,'    ',X,'      ');
  141.     Writeln(MinY,'    ',MaxY,'    ',Y,'      ');
  142.     GetButton;
  143.     if B1=1 then Done:=true;
  144.     if KeyPressed then Done:=true;
  145.   end;
  146.   if KeyPressed then k:=ReadKey;
  147.   X:=round(((X-MinX)/MaxX)*100);  {Percent-adjust:  this scales }
  148.   Y:=round(((Y-MInY)/MaxY)*100);  { the number to between 1 and 100.}
  149.   MX:=X; MY:=Y;
  150.   done:=false;
  151.   gotoxy(1,8);
  152.   Write('Press any key.');
  153.   if KeyPressed then k:=Readkey;
  154.   while not done do begin
  155.     GetJoy;
  156.     X:=round(((X-MinX)/MaxX)*100);
  157.     Y:=round(((Y-MInY)/MaxY)*100);
  158.     gotoxy(1,9);
  159.     GetButton;
  160.     Write(X,'    ',Y,'    ',B1,B2,B3,B4,'      ');
  161.     if keypressed then Done:=true;
  162.   end;
  163.   k:=Readkey;
  164.   CX:=40; CY:=10;  {Initial cursor position}
  165.   D:=100;
  166.   Done:=False;
  167.   ClrScr;
  168.   Writeln('Use the joystick to change cursor positions.');
  169.   Writeln('buttons 1 and 2 write smiley faces,');
  170.   Writeln('buttons 3 and 4 change joystick speed.');
  171.   writeln('Press any key to exit.');
  172.   while not done do begin;
  173.     GetJoy;
  174.     X:=round(((X-MinX)/MaxX)*100);
  175.     Y:=round(((Y-MInY)/MaxY)*100);
  176.     if X>MX+10 then CX:=CX+1;  {change cursor position?}
  177.     if X<MX-10 then CX:=CX-1;
  178.     if Y>MY+10 then CY:=CY+1;
  179.     if Y<MY-10 then CY:=CY-1;
  180.     if CX>80 then CX:=1;  {there is probably a faster way }
  181.     if CX<1 then CX:=80;  {to do this using mod. }
  182.     if CY>23 then CY:=1;
  183.     if CY<1 then CY:=23;
  184.     gotoxy(1,24);
  185.     write(D,'   ');
  186.     gotoxy(CX,CY);
  187.     GetButton;
  188.     if b1=1 then write(chr(1));
  189.     if b2=1 then write(chr(2));
  190.     if b3=1 then D:=(D+1)mod 250;
  191.     if b4=1 then D:=(D-1)mod 250;
  192.     if Keypressed then done:=true;
  193.     delay(D);
  194.   end;
  195.   k:=Readkey;
  196. end.  
  197.          And before I did this, I couldn't progam more than 2 lines of Asm.
  198.  
  199. --- Renegade v10-05 Exp
  200.  * Origin: The Digital Domain - (716) 791-4849 (1:260/149)
  201. SEEN-BY: 270/101 280/1 396/1 3615/50 51
  202. PATH: 260/149 10 1 270/101 396/1 3615/50
  203.